<?php

class UserTagModel
{
    private $autoUidModel;
    private $tagsKeyPrefix       =   'TAG/';
    private $userTagKeyPrefix    =   'UT/';
    private $tagUserKeyPrefix    =   'TUR/';
    private $userInfoKeyPrefix   =   'UI/';
    
    public function __construct(){
        $this->autoUidModel         =   new AutoUidModel();
    }
    
    /**
     * 获取全部标签
     * @param unknown $start
     * @param unknown $length
     */
    public function getAllTags($start, $length){
        $key       =   $this->tagsKeyPrefix. 0;
        $orderBy   = ' id DESC ';
    
        $result    =   Storage::getManyData($key, $orderBy, $start, $length);
        return $result;
    }
    
    /**
     * 通过标签搜索用户
     * @param unknown $tag
     * @param unknown $start
     * @param unknown $length
     * @return boolean|Ambigous <unknown, boolean, multitype:>
     */
    public function searchUserByTagName($tag, $start, $length, $id, $uid, $tagId = 0){
        if (empty($tagId)){
            $tagId = $this->getTagIdByName($tag);
            if (empty($tagId)) {
                return array();
            } 
        }
    	
    	$key        = $this->tagUserKeyPrefix. $tagId;
    	$where      = " tag_id = ".$tagId." AND uid != ".$uid;
    	// 分页
    	if ($id > 0){
    	    $start = 0;
    	    $where = " id < ".$id." AND tag_id = ".$tagId." AND uid != ".$uid;
    	}
    	$orderBy    = ' id DESC ';
    	$result     =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);

    	return $result;
    }
    
    /**
     * 修改用户标签
     * @param unknown $uid
     * @param unknown $tagStr
     * @return boolean
     */
    public function editUserTags($uid, $tagStr){
        if ($uid < 1){
            return FALSE;
        }
        
        // 判断标签字段是否设置值
        if (!empty($tagStr)){
        	$requireTags = array_slice(explode(',', $tagStr), 0, 5);
        }
    	
    	$key        =   $this->tagsKeyPrefix. 0;
    	$userTagKey =   $this->userTagKeyPrefix. $uid;
    	
    	// 获得用户所有的tags
    	$userTags   = $this->getUserAllTags($uid);
    	if (!empty($userTags)){
    	    // 根据tagId分表删除用户的所有tag
    	    foreach ($userTags as $val){
    	        $this->delUserTagsByHashTagId($uid, $val['tag_id']);
    	    }
    	    
    	    // 根据uid分表删除用户的所有tag
    	    $this->delUserTagsByHashUid($uid);
    	}
    	
    	// 重新写入
    	$inserts    = array();
    	$insertTags = array();
    	if ($requireTags){
    	    foreach ($requireTags as $val){
    	        $tag = trim($val);
    	        if (empty($tag)){
    	            continue;
    	        }
    	        
    	        // 屏蔽直接从数据库中获取的接口
    	        // $tagId = $this->getTagIdByName($tag);
    	        
    	        // 获取tag_id
    	        $tagInfo = Tags::getTagInfo($tag);
    	        $tagId = $tagInfo['id'];
    	        
    	        if ($tagId){
    	            $this->addTagByHashUid($uid, $tagId);
    	            $insertId = $this->addTagByHashtagId($uid, $tagId);
    	            if ($insertId){
    	                $inserts[] = $insertId;
    	            }
    	            
        	        //$this->frequencyUpdateTagCount($tagId);
        	        $insertTags[] = $tag;
    	        }
    	        
    	    }
    	}

    	// 更新用户tag文本
    	$tagText = implode(',', $insertTags);
    	$this->updateUserTags($uid, $tagText);
    	
    	return TRUE;
    }
    
    /**
     * 获取用户全部标签
     * @param unknown $uid
     * @return Ambigous <unknown, boolean, multitype:>
     */
    public function getUserAllTags($uid){
        if ($uid < 1){
            return FALSE;
        }
        
        $key       =   $this->userTagKeyPrefix. $uid;
        $where     =   array('uid' => $uid);
        $result =   Storage::getConditionManyData($key, $where);
        return $result;
    }
    
    /**
     * 根据uid分表添加用户全部标签
     * @param unknown $uid
     * @param unknown $tagId
     * @return Ambigous <unknown, boolean, number, resource>
     */
    private function addTagByHashUid($uid, $tagId){
        if ($uid < 1 || $tagId < 1){
            return FALSE;
        }
        
        $key       =   $this->userTagKeyPrefix. $uid;
        $data      =   array('tag_id'=>$tagId, 'uid'=> $uid);
        $result    =   Storage::addData($key, $data);
        return $result;
    }
    
    /**
     * 根据tagId分表添加用户全部标签
     * @param unknown $uid
     * @param unknown $tagId
     * @return Ambigous <unknown, boolean, number, resource>
     */
    private function addTagByHashtagId($uid, $tagId){
        if ($uid < 1 || $tagId < 1){
            return FALSE;
        }
        
        $key       =   $this->tagUserKeyPrefix. $tagId;
        $data      =   array('tag_id'=>$tagId, 'uid'=> $uid);
        $result    =   Storage::addData($key, $data);
        return $result;
    }
    
    /**
     * 根据tagId分表删除用户全部标签
     * @param unknown $uid
     * @param unknown $tagId
     * @return Ambigous <unknown, boolean, multitype:>
     */
    private function delUserTagsByHashTagId($uid, $tagId){
        if ($uid < 1 || $tagId < 1){
            return FALSE;
        }
        
        $key       =   $this->tagUserKeyPrefix. $tagId;
        $where     =   array('uid' => $uid);
        $result    =   Storage::delConditionData($key, $where);
        return $result;
    }
    
    /**
     * 根据uid分表删除用户全部标签
     * @param unknown $uid
     * @param unknown $tagId
     * @return Ambigous <unknown, boolean, multitype:>
     */
    private function delUserTagsByHashUid($uid){
        if ($uid < 1){
            return FALSE;
        }
        
        $key       =   $this->userTagKeyPrefix. $uid;
        $where     =   array('uid' => $uid);
        $result    =   Storage::delConditionData($key, $where);
        return $result;
    }
    
    /**
     * 更新用户标签
     * @param unknown $uid
     * @param unknown $tagsStr
     * @return Ambigous <boolean, unknown, number, resource>
     */
    private function updateUserTags($uid, $tagsStr){
        if ($uid < 1){
            return FALSE;
        }
        
        $key     =    $this->userInfoKeyPrefix . $uid;
        $data    =    array('tags' => $tagsStr);
        $where   =    array('uid' => $uid); 
        $result  =    Storage::setConditionData($key, $data, $where);
        return $result;
    }
    
    /**
     * 根据名称获得标签id
     * @param unknown $name
     * @return Ambigous <unknown, boolean, number, resource>|Ambigous <>
     */
    private function getTagIdByName($name, $need_insert = FALSE){
        if (strlen($name) < 1){
            return FALSE;
        }
        
    	$key    = $this->tagsKeyPrefix. 0;
    	$where  = array('name' => $name);
        $result = Storage::getConditionData($key, $where);
        $id     = 0;
        if ($result) {
            $id = $result['id'];
        }
        
        // 写入数据库
        if (empty($id) && $need_insert){
            $data = array('name' => $name);
            $id = Storage::addData($key, $data);
        }
    	
    	return $id;
    }
 
    /**
     * 更新标签的计数
     * @param unknown $tagId
     */
    private function frequencyUpdateTagCount($tagId) {
    	// 每过100秒
    	if (time() % 100 != 0) {
            return FALSE;
    	}
    	
    	if ($tagId < 1){
    	    return FALSE;
    	}
    	
    	$tagCount = $this->countTagUserNums($tagId);
    	
    	$key      = $this->tagsKeyPrefix. 0;
    	$where    = array('id' => $tagId);
    	$data     = array('count' => $tagCount);
    	$result   = Storage::setConditionData($key, $data, $where);
    	return $result;
    }
    
    /**
     * 获取某个标签用户计数
     * @param unknown $tagId
     * @return Ambigous <Num, boolean, number>
     */
    private function countTagUserNums($tagId){
        if ($tagId < 1){
            return FALSE;
        }
        
        $key    = $this->tagUserKeyPrefix. $tagId;
        $where  = array('tag_id'=>$tagId);
        $result = Storage::countDataRow($key, $where);
        return $result;
    }
}